/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.oszc.bbhmlibrary.utils.properties;

import ohos.agp.utils.Color;
import ohos.global.resource.Element;
import ohos.global.resource.NotExistException;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;
import ohos.global.resource.solidxml.Pattern;
import ohos.global.resource.solidxml.TypedAttribute;

import java.io.IOException;
import java.util.HashMap;

public class PatternElementHelper {
    public interface AttributeObserver<T> {
        void onGetAttribute(T object);
    }

    private HashMap<String, TypedAttribute> patternHash;

    public PatternElementHelper(ResourceManager resourceManager, int res) {
        try {
            Element arcProgressElement = resourceManager.getElement(res);
            Pattern pattern = arcProgressElement.getPattern();
            patternHash = pattern.getPatternHash();
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
    }

    private boolean isStyleAttrsExist() {
        return patternHash != null;
    }

    public <T> void getTypeAttribute(String attrName, int type, AttributeObserver attributeObserver) {
        if (!isStyleAttrsExist()) {
            attributeObserver.onGetAttribute(null);
            return;
        }
        TypedAttribute typedAttribute = patternHash.get(attrName);
        if (typedAttribute == null) {
            attributeObserver.onGetAttribute(null);
            return;
        }
        try {
            switch (type) {
                case TypedAttribute.BOOLEAN_ATTR:
                    attributeObserver.onGetAttribute(typedAttribute.getBooleanValue());
                    return;
                case TypedAttribute.COLOR_ATTR:
                    String colorHex = typedAttribute.getOriginalValue().replace("#","");
                    Color result = new Color((int)Long.parseLong(colorHex,16));
                    attributeObserver.onGetAttribute(result);
                    //attributeObserver.onGetAttribute(new Color(typedAttribute.getColorValue()));
                    return;
                case TypedAttribute.FLOAT_ATTR:
                    attributeObserver.onGetAttribute(typedAttribute.getFloatValue());
                    return;
                case TypedAttribute.INTEGER_ATTR:
                    attributeObserver.onGetAttribute(typedAttribute.getIntegerValue());
                    return;
                case TypedAttribute.STRING_ATTR:
                    attributeObserver.onGetAttribute(typedAttribute.getStringValue());
                    return;
                case TypedAttribute.MEDIA_ATTR:
                    attributeObserver.onGetAttribute(typedAttribute.getMediaValue());
                    return;
                case TypedAttribute.LAYOUT_ATTR:
                    attributeObserver.onGetAttribute(typedAttribute.getLayoutValue());
                    return;
                case TypedAttribute.PATTERN_ATTR:
                    attributeObserver.onGetAttribute(typedAttribute.getPatternValue());
                    return;
                case TypedAttribute.GRAPHIC_ATTR:
                    attributeObserver.onGetAttribute(null);
                    return;
            }
        } catch (IOException e) {
            e.printStackTrace();
            attributeObserver.onGetAttribute(null);
        } catch (NotExistException e) {
            e.printStackTrace();
            attributeObserver.onGetAttribute(null);
        } catch (WrongTypeException e) {
            e.printStackTrace();
            attributeObserver.onGetAttribute(null);
        }
    }

}
